package com.suwish.pc.ui.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.suwish.pc.adb.AdbManager;
import com.suwish.pc.adb.Command;
import com.suwish.pc.ui.component.UIDialog;
import com.suwish.pc.util.StringUtils;

/**
 * 
 * 查看APK的信息，目前非常简单-.-
 * 
 * @author Min
 *
 */
public class ApkDetailDialog extends UIDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

//	private Window parent = null;
	private String apkPath = null;
	private JTextArea detailEditor = null;
	public ApkDetailDialog(Window parent, String path){
		super(parent, "APK Detail");
		
		apkPath = path;
		setModal(true);
		
		initUI();
		initData();
		initEvent();
		
		setSize(new Dimension(680, 500));
		setLocationRelativeTo(parent);
	}
	private void initEvent(){
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				dispose();
			}			
		});
	}
	private void initData(){
		if(StringUtils.isEmpty(apkPath))return;
		try{
			String value = AdbManager.queryResult(new Command(AdbManager.AAPT_PATH, 
					new String[]{"dump", "badging" , StringUtils.parseCmdPath(apkPath)}), true);
			detailEditor.setText(value);
		}catch(Exception e){
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
	private void initUI(){
		JPanel mainPanel = getIContentPane();
	    mainPanel.setLayout(new BorderLayout());
	    mainPanel.add(new JLabel("Detail"), BorderLayout.NORTH);
	    
	    detailEditor = new JTextArea();
	    detailEditor.setEditable(false);
	    mainPanel.add(new JScrollPane(detailEditor), BorderLayout.CENTER);
	    JPanel buttomPane = new JPanel(new FlowLayout(FlowLayout.CENTER));
	    mainPanel.add(buttomPane, BorderLayout.SOUTH);
	    JButton closeButton = new JButton("Close");
	    buttomPane.add(closeButton);
	    closeButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
				
			}
		});
	}
}
